
import { ADD_TAG, MODIFY_TAG, REMOVE_TAG } from "redux/actiontypes";
const InputTagList: TagType[] = [
  { tagName: "衣", TagIcon: "ClothIcon" },
  { tagName: "食", TagIcon: "FoodIcon" },
  { tagName: "住", TagIcon: "HouseIcon" },
  { tagName: "行", TagIcon: "CarIcon" },
];

const OuputTagList: TagType[] = [
  { tagName: "工资", TagIcon: "SalaryIcon" },
  { tagName: "奖金", TagIcon: "BonusIcon" },
];

const initialState: TagState = [
  { category: "0", tags: InputTagList },
  { category: "1", tags: OuputTagList },
];
export default function (state = initialState, action: any) {
  switch (action.type) {
    case ADD_TAG:
      const { category, tag } = action.payload;

      return state.map((_tag) => {
        if (_tag.category === category) {
          _tag.tags.push(tag);
        }
        return _tag;
      });
    case MODIFY_TAG:
      const { tagName, tagIcon, i, oldTagName } = action.payload;
      const newState = state.map((_tag) => {
        if (_tag.category === i) {
          return {
            category: i,
            tags: _tag.tags.map((tag) => {
              if (tag.tagName === oldTagName) {
                return {
                  tagName,
                  TagIcon: tagIcon,
                };
              }
              return tag;
            }),
          };
        }
        return _tag;
      });
      console.log(newState);

      return newState;
    
    case REMOVE_TAG:
      const { tName, cate } = action.payload;
      return state.map((_tag) => {
        if (_tag.category === cate) {
          const cTag = _tag.tags.find((tag) => tag.tagName === tName)
          let idx = _tag.tags.indexOf(cTag!)
          _tag.tags.splice(idx,1)
          return {
            category: cate,
            tags: _tag.tags
          }
        }
        return _tag
      })

    default:
      return state;
  }
}
